package com.youlu.campus.service.jinshuju;

import com.youlu.campus.entity.jinshuju.JinUserInviteRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author : [dengbo]
 * @className : JinShuJuUserInviteService
 * @description : [金数据邀请]
 * @createTime : [2023/6/19 14:39]
 */
@Service
@Slf4j
public class JinShuJuUserInviteService {

    @Autowired
    private MongoTemplate mongoTemplate;

    public JinUserInviteRecord queryByUserInviteInfo(String activityId, String invitedByUserId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("activityId").is(activityId));
        query.addCriteria(Criteria.where("invitedByUserId").is(invitedByUserId));
        return mongoTemplate.findOne(query, JinUserInviteRecord.class);
    }

    public JinUserInviteRecord queryToUserInviteInfo(String activityId,String invitedToUserId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("activityId").is(activityId));
        query.addCriteria(Criteria.where("invitedToUserId").is(invitedToUserId));
        return mongoTemplate.findOne(query, JinUserInviteRecord.class);
    }

    public boolean addInviteRecord(String activityId,String invitedByUserId,String invitedToUserId) {
        // 添加查询判断,主要是后台有报名信息删除
        JinUserInviteRecord record = queryToUserInviteInfo(activityId,invitedToUserId);
        if(record != null) {
            return false;
        }
        record = new JinUserInviteRecord();
        record.setActivityId(activityId);
        record.setInvitedByUserId(invitedByUserId);
        record.setInvitedToUserId(invitedToUserId);
        record.setCreatedTime(new Date());
        mongoTemplate.insert(record);
        return true;
    }
}
